package com.school.manager.controller;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.school.manager.common.Constants;
import com.school.manager.domaim.SysPermMenu;
import com.school.manager.domaim.SysPermission;
import com.school.manager.domaim.SysRolePerm;
import com.school.manager.entity.ResponseEntity;
import com.school.manager.entity.po.SysPermMenusPo;
import com.school.manager.entity.po.SysPermissionPo;
import com.school.manager.entity.vo.SysPermissionVo;
import com.school.manager.enums.ResponseEnum;
import com.school.manager.enums.StatusEnum;
import com.school.manager.service.ISysPermMenuService;
import com.school.manager.service.ISysPermissionService;
import com.school.manager.service.ISysRolePermService;
import com.school.manager.utils.ConvertUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * description：系统权限控制器
 * date：2021/10/133:18 下午
 */

@RestController
@Api(tags = "系统权限")
@RequestMapping(Constants.PROGRAM_PATH + "permission")
public class SysPermissionController {
	
	@Autowired
    private ISysPermissionService sysPermissionService;
	
	@Autowired
	private ISysRolePermService rolePermService;
	
	@Autowired
	private ISysPermMenuService permMenuService;

	@PostMapping("add")
    @ApiOperation("新增系统权限")
    @ApiImplicitParam(name = "系统权限", value = "SysPermissionPo", dataTypeClass = SysPermissionPo.class)
    public ResponseEntity add(@RequestBody SysPermissionPo sysPermissionPo) {
        return ResponseEnum.SUCCESS.success(sysPermissionService.save(ConvertUtil.convertObj(sysPermissionPo, SysPermission.class)));
    }


    @PostMapping("edit")
    @ApiOperation("修改系统权限")
    @ApiImplicitParam(name = "系统权限", value = "SysPermissionPo", dataTypeClass = SysPermissionPo.class)
    public ResponseEntity edit(@RequestBody SysPermissionPo sysPermissionPo) {
        return ResponseEnum.SUCCESS.success(sysPermissionService.updateById(ConvertUtil.convertObj(sysPermissionPo, SysPermission.class)));
    }


    @GetMapping("get/{id}")
    @ApiOperation("系统权限详情")
    @ApiImplicitParam(name = "系统权限主键", value = "id", dataTypeClass = String.class)
    public ResponseEntity get(@PathVariable("id") String id) {
        SysPermission sysPermission = sysPermissionService.getById(id);
        return ResponseEnum.SUCCESS.success(ConvertUtil.convertObj(sysPermission, SysPermissionVo.class));
    }
    
    @GetMapping("del/{id}")
    @ApiOperation("系统权限删除")
    @ApiImplicitParam(name = "系统权限主键", value = "id", dataTypeClass = String.class)
    public ResponseEntity del(@PathVariable("id") String id) {
    	// 删除权限
    	sysPermissionService.removeById(id);
    	// 删除菜单关联
    	permMenuService.remove(new LambdaQueryWrapper<SysPermMenu>()
    			.eq(SysPermMenu::getPermId, id));
        return ResponseEnum.SUCCESS.success();
    }


    @PostMapping("page")
    @ApiOperation("系统权限分页列表")
    @ApiImplicitParam(name = "系统权限Po", value = "SysPermissionPo", dataTypeClass = SysPermissionPo.class)
    public ResponseEntity page(@RequestBody SysPermissionPo SysPermissionPo) {
        return ResponseEnum.SUCCESS.success(sysPermissionService.page(SysPermissionPo));
    }
    
    @GetMapping("getbyrole/{id}")
    @ApiOperation("根据权限id获取权限列表")
    @ApiImplicitParam(name = "系统权限主键", value = "id", dataTypeClass = String.class)
    public ResponseEntity getByRole(@PathVariable("id") String roleId) {
    	List<SysRolePerm> rolePerms = rolePermService.list(new LambdaQueryWrapper<SysRolePerm>()
    			.eq(SysRolePerm::getRoleId, roleId)
    			.eq(SysRolePerm::getStatus, StatusEnum.ENABLE.getCode()));
    	List<String> permIds = rolePerms.stream().map(SysRolePerm::getPermId).collect(Collectors.toList());
        Collection<SysPermission> perms = sysPermissionService.listByIds(permIds);
        return ResponseEnum.SUCCESS.success(ConvertUtil.convertList(perms, SysPermissionVo.class));
    }
    
    @PostMapping("setting")
    @ApiOperation("设置系统权限菜单")
    @ApiImplicitParam(name = "权限菜单Po", value = "sysPermMenusPo", dataTypeClass = SysPermMenusPo.class)
    public ResponseEntity setting(@RequestBody SysPermMenusPo sysPermMenusPo) {
    	// 删除原有菜单
    	permMenuService.remove(new LambdaQueryWrapper<SysPermMenu>()
    			.eq(SysPermMenu::getPermId, sysPermMenusPo.getPermId()));
    	// 新增菜单关联
    	List<SysPermMenu> permMenus = sysPermMenusPo.getMenus().stream().map(id -> {
    		SysPermMenu permMenu = new SysPermMenu();
    		permMenu.setPermId(sysPermMenusPo.getPermId());
    		permMenu.setMenuId(id);
    		return permMenu;
    	}).collect(Collectors.toList());
        return ResponseEnum.SUCCESS.success(permMenuService.saveBatch(permMenus));
    }
    
}
